#!/bin/env bash
zk_dir=/data/zk
zk_data_dir=${zk_dir}/data
zk_tar=${zk_dir}/apache-zookeeper-3.5.9-bin.tar.gz
zk_original_dir=${zk_dir}/apache-zookeeper-3.5.9-bin
zk_home=${zk_dir}/zk
zk_config_dir=${zk_home}/conf
zk_cluster="192.168.1.50 192.168.1.51 192.168.1.99"
local_ip=$(cat <"/etc/sysconfig/network-scripts/ifcfg-ens33" | grep IPADDR | awk -F "=" \{'print $2'\} | sed 's/\"//g')
usr=root

echo "current host ip is ${local_ip}"

mk_zk_dir() {
    if [ ! -d "${zk_dir}" ]; then
        echo "mk zk dir"
        mkdir -p ${zk_dir}
    fi
}

mk_zk_data_dir(){
    if [ ! -d "${zk_data_dir}" ]; then
        echo "mk zk data dir"
        mkdir -p ${zk_data_dir}
    fi
}

tar_zk() {
    if [ -f "${zk_tar}" ] && [ ! -d "${zk_home}" ]; then
        echo "tar zk"
        tar xf ${zk_tar} -C ${zk_dir}
    fi
}

alias_zk_dir() {
    if [ ! -d "${zk_home}" ]; then
        echo "alias zk dir"
        mv ${zk_original_dir} ${zk_home}
    fi
}

zk_cfg() {
    echo "write zk cfg"
    cat <<EOF >${zk_config_dir}/zoo.cfg
tickTime=2000
dataDir=${zk_data_dir}
clientPort=2181
initLimit=20
syncLimit=5
EOF
}

append_cluster_ip_to_zkcfg() {
    zk_cluster_array=("${zk_cluster}")
    for i in "${!zk_cluster_array[@]}"; do
        echo "server.${i}=${zk_cluster_array[${i}]}:2888:3888" >>${zk_config_dir}/zoo.cfg
    done
}

write_zkcfg(){
    zk_cfg
    append_cluster_ip_to_zkcfg
}

scp_zk_file() {
    for ip in ${zk_cluster}; do
        if [ "${local_ip}" != "${ip}" ]; then
            connected=$(ssh ${usr}@"${ip}" "[ -d /data/zk ] && echo true || mkdir -p /data")
            if [ "${connected}" != "true" ]; then
                echo "scp zk file to ${ip}"
                scp -qr ${zk_dir} ${usr}@"${ip}":${zk_dir}
                echo "ok" 
            fi
        fi
    done
}

write_myid_file(){
    if [ ! -f "${zk_data_dir}/myid" ]; then
        echo "0" > ${zk_data_dir}/myid && echo "write myid ok"
    fi
    zk_cluster_array=("${zk_cluster}")
    ssh ${usr}@"${zk_cluster_array[1]}" "[ -f /data/zk/data/myid ] || (echo 1 > /data/zk/data/myid && echo write myid ok)"
    ssh ${usr}@"${zk_cluster_array[2]}" "[ -f /data/zk/data/myid ] || (echo 2 > /data/zk/data/myid && echo write myid ok)"
}

start_zk(){
    if [ "" == "$(jps | awk '{ print $2 }' | grep QuorumPeerMain)" ]; then
        for ip in ${zk_cluster}; do
            if [ "${local_ip}" != "${ip}" ]; then
                echo "start zk of ${ip}"
                ssh ${usr}@"${ip}" "/data/zk/zk/bin/zkServer.sh start"
            else
                echo "start zk of ${ip}"
                ${zk_home}/bin/zkServer.sh start
            fi
        done
    fi
}

echo_color(){
    echo -e "\033[7;46;30m$1\033[0m" 
}

zk_status(){
    for ip in ${zk_cluster}; do
        echo_color "status zk of ${ip}"
        if [ "${local_ip}" != "${ip}" ]; then
            ssh ${usr}@"${ip}" "/data/zk/zk/bin/zkServer.sh status"
        else
            ${zk_home}/bin/zkServer.sh status
        fi
    done
}

setup_zk(){
    mk_zk_dir
    mk_zk_data_dir
    tar_zk
    alias_zk_dir
    write_zkcfg
    scp_zk_file
    write_myid_file
    start_zk
    zk_status
}

usage() {
    echo "useage:"
    echo "-h help"
    echo "-z setup"
    exit 0
}

while getopts 'hz' OPT; do
    case $OPT in
    z) setup_zk "$OPTARG";;
    h) usage;;
    ?) usage;;
    esac
done